package com.example.demo.mapper;

import com.example.demo.entity.DeliveryRecord;
import com.example.demo.entity.JobPosition;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mapper
public interface Look_for_jpb_Mapper {
    ArrayList<JobPosition> select_Look_for_jon();

    int add_job(JobPosition jobPosition);


    Page<JobPosition> select_Look_for_jon_page(Integer page, Integer size,Integer hrId,JobPosition jobPosition);

    int delete_job(Integer id);

    int update_job(JobPosition jobPosition);


    /**
     * 插入投递记录
     */
    @Insert("INSERT INTO delivery_records(user_id, job_id, hr_id, resume_path) VALUES(#{userId}, #{jobId}, #{hrId}, #{resumePath})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertDeliveryRecord(DeliveryRecord deliveryRecord);

    /**
     * 根据用户ID查询已投递的岗位ID列表
     */
    @Select("SELECT job_id FROM delivery_records WHERE user_id = #{userId}")
    List<Integer> selectDeliveredJobIdsByUserId(@Param("userId") Integer userId);

    /**
     * 检查是否已投递过某个岗位
     */
    @Select("SELECT COUNT(*) FROM delivery_records WHERE user_id = #{userId} AND job_id = #{jobId}")
    int countByUserIdAndJobId(@Param("userId") Integer userId, @Param("jobId") Integer jobId);




    @Select("SELECT job_id FROM delivery_records WHERE user_id = #{userId}")
    List<Integer> getDeliveryJobIdsByUserId(@Param("userId") Integer userId);

    @Select("SELECT * FROM delivery_records WHERE user_id = #{userId}")
    List<DeliveryRecord> getDeliveryRecordsWithDetailsByUserId(@Param("userId") Integer userId);

    /**
     * 根据HR ID查询收到的简历投递记录
     */
    @Select("SELECT * FROM delivery_records WHERE hr_id = #{hrId} ORDER BY deliver_time DESC")
    List<DeliveryRecord> getDeliveryRecordsByHrId(@Param("hrId") Integer hrId);

    /**
     * 联表查询获取HR收到的简历详情，包含用户信息和岗位信息
     */
    @Select("SELECT d.user_id as userId, d.job_id as jobId, d.deliver_time as deliverTime, u.Name as userName, u.Email as userEmail, u.Phone as userPhone, u.Resume as userResume, " +
            "j.name as jobName, j.companyId as companyId " +
            "FROM delivery_records d " +
            "LEFT JOIN user u ON d.user_id = u.UserID " +
            "LEFT JOIN jobposition j ON d.job_id = j.jobId " +
            "WHERE d.hr_id = #{hrId} " +
            "ORDER BY d.deliver_time DESC")
    List<Map<String, Object>> getDeliveryRecordsWithDetailsByHrId(@Param("hrId") Integer hrId);

    List<JobPosition> getJobsByJobIds(@Param("jobIds") List<Integer> jobIds);
}
